<template>
    <div>
        <el-container style="height: 500px; border: 1px solid #eee">
            <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
                <el-menu router :default-openeds="['0']">
                    <el-submenu v-for="(item,index) in $router.options.routes" :index="index+''" v-if="item.show">
                        <template slot="title"><i class="el-icon-message"></i>{{item.name}}</template>
                        <el-menu-item v-for="item2 in item.children" :index="item2.path"
                                      :class="$route.path==item2.path?'is-active':''">{{item2.name}}</el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="ID" prop="bookID">
                    <el-input v-model="ruleForm.bookID" readonly></el-input>
                </el-form-item>
                <el-form-item label="书名" prop="bookName">
                    <el-input v-model="ruleForm.bookName"></el-input>
                </el-form-item>
                <el-form-item label="作者" prop="bookCounts">
                    <el-input v-model="ruleForm.bookCounts"></el-input>
                </el-form-item>
                <el-form-item label="详情" prop="detail">
                    <el-input v-model="ruleForm.detail"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-container>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    bookID:'',
                    bookName: '',
                    bookCounts: '',
                    detail: '',
                },
                rules: {
                    bookName: [
                        { required: true, message: '请输入书名', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    bookCounts: [
                        { required: true, message: '请输入作者', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    detail: [
                        { required: true, message: '请输入详情', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {

            submitForm(formName) {
                const _this = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(_this.ruleForm)
                        axios.put('/book/update',_this.ruleForm).then(function (resp) {
                            console.log(resp);
                            if (resp.data=='success'){
                                _this.$alert('修改成功', '修改成功', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/PageOne');
                                    }
                                });

                            }
                        });
                        //alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        created() {
            const _this = this;
            axios.get('/book/get/'+this.$route.query.bookID)
                .then(function (resp) {
                    _this.ruleForm = resp.data;
                    console.log(_this.ruleForm);
            })
        }
    }
</script>